package com.wxg.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class MyUtils {

	public static String userdir(){
		String usrdir = System.getProperty("user.dir");
		return usrdir;
	}
	
	public static Properties loadProp(String propFilePath){
		FileInputStream fis = null;
		Properties prop = new Properties();
		try {
			fis = new FileInputStream(propFilePath);
			prop.load(fis);
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return prop;
	}
	
	/**
	 * 自己写一个读取配置文件的类
	 * 比使用Java自带的读取`Properties`文件的类要灵活很多
	 * 貌似JFinal中也是自己定制的读取Properties类
	 * @param propFilePath
	 * @return
	 */
	public static Prop readProp(String propFilePath){
		Prop map = new Prop();
		BufferedReader br = null;
		try {
			br = new BufferedReader( new FileReader(propFilePath) );
			String line = null;
			while( (line=br.readLine()) != null ){
				if( line.startsWith("#") ){
					continue;
				}
				String[] arr = line.split("=");
				if( arr.length == 2 ){
					map.put(arr[0].trim(), arr[1].trim().replaceAll("\\\\", "/"));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(null != br) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return map;
	}
	
	/**
	 * 只是为了少写几个字符而已
	 * `HashMap`的子类,key和value都是字符串.
	 * @author wxg
	 *
	 */
	public static class Prop extends HashMap<String,String> {
		private static final long serialVersionUID = -5233357463636977257L;
	}
	
	
}
